/*
 * Created by JFormDesigner on Mon Jan 02 21:54:09 CST 2023
 */

package otherSAMS.ui;

import otherSAMS.dao.CourseSelectionDao;
import otherSAMS.model.CourseScore;
import otherSAMS.util.ListToArray;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

/**
 * @author txx
 */
public class TeacherScore extends JFrame {
    public TeacherScore() {
        initComponents();
    }

    public void UserLogin() {
        initComponents();
    }

    public static void main(String[] args) {
        TeacherScore teacher = new TeacherScore();
        teacher.setVisible(true);
    }

    private void selectMouseClicked(MouseEvent e) {
        // TODO add your code here
        String studentName = studentNameField.getText().trim();
        CourseSelectionDao courseSelectionDao = new CourseSelectionDao();
        List<CourseScore> courseScoreList = courseSelectionDao.selectByStudentName(studentName);
        Object[][] data;
        if (courseScoreList != null && courseScoreList.size() > 0) {
            data = ListToArray.listToArrayWay(courseScoreList);
        } else {
            JOptionPane.showMessageDialog(this, "无结果");
            data = new String[0][];
        }
        table1.setModel(new DefaultTableModel(
                data, tableColumeTitle
        ));
        scrollPane1.setViewportView(table1);
    }

    private void InputScoreMouseClicked(MouseEvent e) {
        // TODO add your code here
        try {
            int studentId = Integer.parseInt(studentIdField.getText().trim());
            int courseId = Integer.parseInt(courseIdField.getText().trim());
            int score = Integer.parseInt(scoreField.getText().trim());
            CourseSelectionDao courseSelectionDao = new CourseSelectionDao();
            JOptionPane.showMessageDialog(this, "录入成功！");
            int id = courseSelectionDao.selectByStudentIdAndCourseId(studentId, courseId);
            if (id == -1) {
                JOptionPane.showMessageDialog(this, "没有需要录入的成绩");
                studentIdField.setText("");
                courseIdField.setText("");
                scoreField.setText("");
                return;

            } else {
                if (courseSelectionDao.updateScoreById(score, id)) {
                    selectMouseClicked(null);
                } else {
                    JOptionPane.showMessageDialog(this, "成绩录入失败");
                }
            }
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "输入的不是纯数字");
        }
    }

    private void button2MouseClicked(MouseEvent e) {
        // TODO add your code here
    }

    private void retur(ActionEvent e) {
        this.dispose();
        UserLogin userLogin = new UserLogin();
        userLogin.initComponents();
        userLogin.setVisible(true);
        // TODO add your code here
    }

    private void exit(ActionEvent e) {
        this.dispose();
        // TODO add your code here
    }

    void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        menuBar1 = new JMenuBar();
        menu1 = new JMenu();
        button3 = new JButton();
        button1 = new JButton();
        panel1 = new JPanel();
        studentNameField = new JTextField();
        label1 = new JLabel();
        select = new JButton();
        label2 = new JLabel();
        studentIdField = new JTextField();
        courseIdField = new JTextField();
        scoreField = new JTextField();
        InputScore = new JButton();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();

        //======== this ========
        setTitle("\u5b66\u751f\u6210\u7ee9\u7ba1\u7406\u7cfb\u7edf");
        setPreferredSize(new Dimension(600, 400));
        setBackground(new Color(0xccffff));
        Container contentPane = getContentPane();

        //======== menuBar1 ========
        {
            menuBar1.setBackground(new Color(0x99ffff));

            //======== menu1 ========
            {
                menu1.setText("\u7528\u6237\u83dc\u5355");
                menu1.setBackground(new Color(0x0066ff));

                //---- button3 ----
                button3.setText("\u8fd4\u56de");
                button3.addActionListener(e -> {
			exit(e);
			retur(e);
		});
                menu1.add(button3);

                //---- button1 ----
                button1.setText("\u9000\u51fa\u7cfb\u7edf");
                button1.addActionListener(e -> exit(e));
                menu1.add(button1);
            }
            menuBar1.add(menu1);
        }
        setJMenuBar(menuBar1);

        //======== panel1 ========
        {
            panel1.setBackground(new Color(0xccffff));

            //---- label1 ----
            label1.setText("\u5b66\u751f\u59d3\u540d");

            //---- select ----
            select.setText("\u67e5\u8be2");
            select.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    selectMouseClicked(e);
                }
            });

            //---- label2 ----
            label2.setText("\u5b66\u53f7");

            //---- InputScore ----
            InputScore.setText("\u5f55\u5165\u6210\u7ee9");
            InputScore.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button2MouseClicked(e);
                    InputScoreMouseClicked(e);
                }
            });

            //---- label3 ----
            label3.setText("\u8bfe\u7a0b\u7f16\u7801");

            //---- label4 ----
            label4.setText("\u5206\u6570");

            //---- label5 ----
            label5.setText("\u5b66\u751f\u6210\u7ee9\u5217\u8868");
            label5.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 22));
            label5.setForeground(Color.blue);

            //======== scrollPane1 ========
            {

                //---- table1 ----
                table1.setModel(new DefaultTableModel(
                    new Object[][] {
                        {"\u5b66\u53f7", "\u59d3\u540d", "\u8bfe\u7a0b\u540d\u79f0", "\u8bfe\u7a0b\u7f16\u7801", "\u5b66\u5206", "\u5206\u6570", "\u5f55\u5165\u65f6\u95f4"},
                        {null, null, null, null, null, null, null},
                    },
                    new String[] {
                        null, null, null, null, null, null, null
                    }
                ));
                table1.setBackground(Color.cyan);
                scrollPane1.setViewportView(table1);
            }

            GroupLayout panel1Layout = new GroupLayout(panel1);
            panel1.setLayout(panel1Layout);
            panel1Layout.setHorizontalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGroup(panel1Layout.createParallelGroup()
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                    .addGroup(panel1Layout.createSequentialGroup()
                                        .addComponent(label2)
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(studentIdField, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE))
                                    .addGroup(panel1Layout.createSequentialGroup()
                                        .addComponent(label1)
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(studentNameField, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)))
                                .addGap(18, 18, 18)
                                .addComponent(select))
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGap(188, 188, 188)
                                .addGroup(panel1Layout.createParallelGroup()
                                    .addComponent(label5)
                                    .addGroup(panel1Layout.createSequentialGroup()
                                        .addComponent(label3)
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(courseIdField, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(label4)
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(scoreField, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE)))))
                        .addGap(18, 18, 18)
                        .addComponent(InputScore)
                        .addContainerGap(66, Short.MAX_VALUE))
                    .addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 583, Short.MAX_VALUE)
            );
            panel1Layout.setVerticalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(label1)
                            .addComponent(studentNameField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addComponent(select))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panel1Layout.createParallelGroup()
                            .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(studentIdField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(label2))
                            .addGroup(GroupLayout.Alignment.TRAILING, panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(label3, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
                                .addComponent(courseIdField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(label4, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
                                .addComponent(scoreField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(InputScore)))
                        .addGap(24, 24, 24)
                        .addComponent(label5, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE))
            );
        }

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addComponent(panel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 6, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("欢迎使用学生成绩管理系统！");

    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JMenuBar menuBar1;
    private JMenu menu1;
    private JButton button3;
    private JButton button1;
    private JPanel panel1;
    private JTextField studentNameField;
    private JLabel label1;
    private JButton select;
    private JLabel label2;
    private JTextField studentIdField;
    private JTextField courseIdField;
    private JTextField scoreField;
    private JButton InputScore;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JScrollPane scrollPane1;
    private JTable table1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
    private String[] tableColumeTitle = {"\u5b66\u53f7", "\u59d3\u540d", "\u8bfe\u7a0b\u540d\u79f0", "\u8bfe\u7a0b\u7f16\u7801", "\u5b66\u5206", "\u5206\u6570", "\u5f55\u5165\u65f6\u95f4"};

}
